﻿/* This is a test to make sure that copyright header and file comments are left as-is 
 * 
 * 
 */
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading;

namespace Test_001
{
    class Program
    {
		private static int testCases;
        private static double a, b, c;
        public static double A, B, C;

        private static int TestVars()
        {
            var aa = a;
            var bb = b;
            var cc = c;
            var dd = A;
            var itest = testCases;

            string[] tests = new[] { "test1", "test2", "test3" };
            var tests2 = tests;
            foreach (var s in tests2)
            {
                Console.WriteLine(s);   
            }

            List<string> test3 = new List<string>(tests);
            var tests4 = test3;
            foreach (var s in tests4)
            {
                Console.WriteLine(s);
            }

            double d123 = 1.23;
            int i123 = 123;

            string temp1 = "We have " + d123 + " and " + i123;
            Console.WriteLine(temp1);

            Double D123 = 1.23;
            Int32 I123 = 123;
            string temp2 = "We have " + D123 + " and " + I123;

            if ( temp1 == temp2 )
                Console.WriteLine("Int32/int & Double/double to string passed");
            else
                Console.WriteLine("Int32/int & Double/double to string FAILED!");

            int failures = 0;
            if (D123 != d123)
                failures++;
            if (I123 != i123)
                failures++;
            if (temp1 != temp2)
                failures++;

            Debug.Assert(failures == 0);

            return failures;
        }

        private static int TestStringFormat()
        {
            Console.WriteLine( string.Format("{0} {1} {0}", 1 , 2) );
            string s = string.Format("{0} {1} {0}", 1, 2);
            Console.WriteLine(s);

            Console.WriteLine(string.Format("{0} {1} {2} {3} {4}", "Why", "would", "you", "do", "this?"));
            Console.WriteLine(string.Format("And the current day is {0} {0} {0}", DateTime.Now.Day));
            return 0;
        }

        static int TestArrays()
        {
            string[] tests = new[] { "test1", "test2", "test3" };
            foreach (string s in tests)
                Console.Write(s + " ");
            Console.WriteLine();
            return 0;
        }

		public 
		double 
		SuccessRate;

        static int TestLists()
        {
            List<string> tests = new List<string>();

            tests.Add("test1");
            tests.Add("test2");
            tests.Add("test3");

            foreach (string s in tests)
                Console.Write(s + " ");
            Console.WriteLine();

            for (int i = 0; i < tests.Count; i++)
            {
                Console.Write(tests[i] + " ");
            }
            Console.WriteLine();
            for (int k = 0; k < tests.Count; k++)
                Console.Write(tests[k] + " ");
            Console.WriteLine();

            string[] ss = tests.ToArray();
            foreach (string sss in ss)
                Console.Write("{0} ", sss);
            Console.WriteLine();

            return 0;
        }

		// test failures
		private int 
		testFailures;

		// this is
		// a 
		// hasing test
        static int TestHashing(
		)
        {
            Dictionary<string,double> grades = new Dictionary<string, double>();

            grades["Tom"] = 3.7;
            grades["Melissa"] = 3.8;
            grades["Jason"] = 2.9;

            Console.WriteLine("Grades are:");
            foreach (KeyValuePair<string, double> d in grades)
            {
                Console.WriteLine(d.Key + " has a GPA of " + d.Value);
            }

            foreach (var d2 in grades)
            {
                Console.WriteLine(d2.Key + " has a GPA of " + d2.Value);
            }

            int failures = 0;
            if (grades["Tom"] != 3.7)
                failures++;
            if (grades["Melissa"] != 3.8)
                failures++;
            if (grades["Jason"] != 2.9)
                failures++;

            Debug.Assert(failures == 0);

            return failures;
        }

		/*
		 * This is a dictionary
		 * test
		 */
        static 
		int
		TestDirectory()
        {
            DirectoryInfo di = new DirectoryInfo("c:\\");
            Console.WriteLine("--- files:");
            foreach (FileInfo fi in di.GetFiles("d*"))
            {
                Console.WriteLine(fi.Name);
            }
            Console.WriteLine("--- directories:");
            foreach ( DirectoryInfo sub in di.GetDirectories("p*") )
            {
                Console.WriteLine(sub.Name);
            }
            return 0;
        }

        static int TestFiles()
        {
            if ( File.Exists("c:\\this-file-should-not-exist.txt"))
                Console.WriteLine("FOUND NON-EXISTENT FILE?");

            string testFile = "c:\\test.txt";
            //File.WriteAllLines(testFile, new[] { "line1", "line2" });
            File.WriteAllText(testFile, "This\r\nis a long\r\nstory\r\nthe end");
            string s = File.ReadAllText(testFile);
            string[] s2 = File.ReadAllLines(testFile);
            File.Delete(testFile);

            return 0;
        }

        static int TestDates()
        {
            DateTime d = DateTime.Now;

            Thread.Sleep(1000);
            TimeSpan ts = DateTime.Now - d;

            Console.WriteLine("{0}ms elapsed", ts.TotalMilliseconds);

            return 0;
        }

        static int TestStrings()
        {
            int failures = 0;
            string s = string.Format("test {0} and {1}", 1, 2);

            string[] s2 = s.Split(' ');
            foreach( string s3 in s2 )
                Console.WriteLine(s3);

			string s4 = "This ";
			s4 += "is ";
			s4 += "too ";
			s4 += "Cool!";
			Console.WriteLine( s4 );

            if (s4 == "This is too Cool!")
                Console.WriteLine("s4 passed!");
            else
            {
                Console.WriteLine("s4 failed!");
                failures++;
            }

            StringBuilder sb = new StringBuilder("This ");
			sb.Append( "is " );
			sb.Append( "too " );
			sb.Append( "Cool!" );
			Console.WriteLine( sb.ToString() );

			string s5 = sb.ToString();
            if (s5 != "This is too Cool!")
            {
                Console.WriteLine("StringBuilder failed!");
                failures++;
            }
            else
                Console.WriteLine("StringBuilder passed!");

            Debug.Assert(s4 == "This is too Cool!");
            Debug.Assert(s5 == "This is too Cool!", "Did not get the expected value for s5");

            return failures;
        }

        static int TestFactorial( int i )
        {
            DateTime start = DateTime.Now;
            double factorial = 1;
            int factor = 1;
            while (factor < i)
                factorial *= factor++;
            TimeSpan ts = DateTime.Now - start;
            Console.WriteLine( "Completed factorial of " + i + " in " + ts.TotalMilliseconds + ": " + factorial);
            return 0;
        }

        private static int TestUnsupportedFeatures()
        {
            int failures = 0;
            try
            {

            }
            catch (Exception)
            {
                Console.WriteLine("C# catch's aren't handled");
                failures++;
            }
            finally
            {
                Console.WriteLine("finally in the finally");
            }

            ArrayList al = new ArrayList();
            Console.WriteLine("ArrayList() isn't supported - rewrite your C# to use generics (i.e. List<string>, List<myobject>, etc...)");

            return failures;
        }


        // GoNative Substitute Source: ..\..\test-001-native\test-001-native\substitution.cpp
        private static int TestSourceCodeSubstitution()
        {
            Console.WriteLine("FAILED - this is the method body implementation from the original C# code.");

            return 1;   // the C# version fails, the C++ substituted version should pass
        }

        public enum Answers
        {
            Yes,
            No,
            Maybe
        };

        private static int TestEnums()
        {
            int failures = 0;

            Answers tom = Answers.Yes;
            if (tom != Answers.Yes)
                failures++;

#if false
            Answers[] everyone = { Answers.Maybe, Answers.No, Answers.Maybe, Answers.Yes };
            if (everyone[0] != Answers.Maybe)
                failures++;
            if (everyone[1] != Answers.No)
                failures++;
            if (everyone[2] != Answers.Maybe)
                failures++;
            if (everyone[3] != Answers.Yes)
                failures++;
#endif

            return failures;
        }

        static void Main(string[] args)
        {
            List<string> args2 = new List<string>();
            args2.Add("hey");

            Console.WriteLine( "Hello!" );

#if true
            // this comment is in an 'active' code section
#endif
#if false
            // this comment is in an 'in-active' code section
#endif
            int failures = 0;

            failures += TestVars();
            failures += TestStringFormat();
            failures += TestArrays();
            failures += TestLists();
            failures += TestHashing();
            failures += TestDirectory();
            failures += TestFiles();
            failures += TestFactorial(10);
            failures += TestFactorial(10000000);
            failures += TestFactorial(20000000);
            //failures += TestFactorial(50000000);
            //failures += TestFactorial(100000000);
            failures += TestDates();
            failures += TestStrings();
            failures += TestUnsupportedFeatures();
            failures += TestSourceCodeSubstitution();
            failures += TestEnums();
            
            Console.WriteLine("\nThere are a total of {0} failures\n(1 expected in C# build, 0 expected in C++ builds\n", failures);

            Console.WriteLine("press the enter key");
            Console.ReadLine();
            Console.WriteLine("Goodbye!");
        }

    }
}
